<%@ page contentType="text/html;charset=utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
	<head>
		<title>临时人员单人分配</title>
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="this is my page">
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">
		<link rel="stylesheet"
			href="<%=request.getContextPath()%>/css/common.css" type="text/css" />

		<!-- 使用struts 2的 datetimepicher 必须有这个头文件 -->
		<s:head />
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/apartmentViewAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/studentLodgeMgrAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/dormitoryAllocateAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/engine.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/util.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/jquery-latest.pack.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/noConflictOfJquery.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/forCSS.js'></script>
		<script type="text/javascript">
		//定义全局变量
		var aprt_id = ''; //公寓id
		var gdorm_id=''; //宿舍id
		var page;   //dwr分页的page对象，注意在页面中是一个Json对象
		var dormCache = { }; //存放宿舍的一个复制
		var apartamount; //定义查询出的公寓总条数
		var gaptInfo=""; //这个gaptInfo是全局变量
		var floorno;
		var totalcount;
		var dormitoryconcretefuncid = 2;//与studentdormitorytype对应,宿舍具体功能代码
		
	//===给apartment的下拉列表赋值	
	function setApartList() {
		return apartmentViewAction.getAllApartInfo(mycallback);
	}
	var mycallback = function setList(aptValue)//回调函数
	{
		gaptInfo =aptValue;
		var s = $("aptId");
		apartamount = gaptInfo.length; //给公寓总条数赋值
		s.options[0] = new Option("请选择公寓",'0');
		for ( var i = 1; i < aptValue.length+1; i++) {
						var opt = new Option(aptValue[i-1].apartmentname,aptValue[i-1].apartmentid);
						s.options[i] = opt;
		}
	}
	
				/**
				* 公寓select框change函数
				*/
		function selectChange(){
					apartmentid = $("aptId").options[$("aptId").selectedIndex].value; //给apartmentid赋值
						//取到了对应apartmentid的楼层数目
						var aprt_floorno;
						var aprt_name;
						for(var m = 0; m<apartamount;m++){
							if(gaptInfo[m].apartmentid ==apartmentid){
							aprt_floorno = gaptInfo[m].apartmentfloornum;
							aprt_name = gaptInfo[m].apartmentname;
							break;
							}
						}
						var sf = $("floorno");
						DWRUtil.removeAllOptions(sf);  
						sf.options[0] = new Option("请选择楼层",'0');
						if(aprt_name == "七号楼"){
						for ( var j = 1; j < (parseInt(aprt_floorno)-1); j++) {
							var optt = new Option((j+2)+"层",(j+2));
							sf.options[j] = optt;
						}
						}
						else if(aprt_name == "三号楼"){
						for ( var j = 1; j < (parseInt(aprt_floorno)); j++) {
							var optt = new Option((j+1)+"层",(j+1));
							sf.options[j] = optt;
						}
						}
						else{
						for ( var j = 1; j < (parseInt(aprt_floorno)+1); j++) {
							var optt = new Option(j+"层",j);
							sf.options[j] = optt;
						}
						
						}
				}

	//根据宿舍名和公寓id获得一个宿舍列表
	function getAvailDormbyAptIdDormName(){
		aprt_id = $("aptId").options[$("aptId").selectedIndex].value;
		if(aprt_id=="0"){
			alert("请选择公寓！");
			return;
		}
		floorno = $("floorno").options[$("floorno").selectedIndex].value;
		var dorm_name = $("dormname").value;
		if(floorno=="0"){
			alert("请选择楼层！");
			return;
		}
		if(dorm_name==""){ //根据公寓id去查list
			searchTotalCount();
		}else{ //公寓+宿舍
		
			if($('dataTable').style.visibility == 'visible'){
				$('dataTable').style.visibility = 'hidden';
			}
			if($('pageTable').style.visibility == 'visible'){
				$('pageTable').style.visibility = 'hidden';
			}
			if($('resultTable').style.visibility == 'visible'){
				$('resultTable').style.visibility = 'hidden';
			}
			
			studentLodgeMgrAction.getDormByApartIdAndDormName(aprt_id,dorm_name,dormitoryconcretefuncid,mygetADormback);
		}
	}



	function searchTotalCount(){
			if($('dataTable').style.visibility == 'visible'){
				$('dataTable').style.visibility = 'hidden';
			}
			if($('pageTable').style.visibility == 'visible'){
				$('pageTable').style.visibility = 'hidden';
			}
			if($('resultTable').style.visibility == 'visible'){
				$('resultTable').style.visibility = 'hidden';
			}
		studentLodgeMgrAction.getTotalAvailDormRowsAction(aprt_id,floorno,dormitoryconcretefuncid,scallback);
	}
	
	var scallback = function s_callback(ros){
		totalcount = ros;
		//alert(totalcount);
		studentLodgeMgrAction.getAvailDromPerPageByApartmentID(1,aprt_id,floorno,dormitoryconcretefuncid,totalcount,mygetAllAvailDormback);
	}
	/**
	* callback函数，获得一个学生宿舍
	*/
	var mygetADormback = function setDormList(dormArray){
		if(dormArray==null){
			if($('dataTable').style.visibility == 'visible'){
				$('dataTable').style.visibility = 'hidden';
			}
			if($('pageTable').style.visibility == 'visible'){
				$('pageTable').style.visibility = 'hidden';
			}
			if($('resultTable').style.visibility == 'visible'){
				$('resultTable').style.visibility = 'hidden';
			}
			alert("查询可用宿舍结果为空！");
			return;
		}
	
		//对可用宿舍列表div隐藏操作
			$('resultTable').style.visibility = 'visible';
			$("selectdormselect").style.display="block"; //自身div显示 
		//alert(dormArray);
		//给div赋值
		$("dorm_id").innerHTML = dormArray[0];
		$("dorm_name").innerHTML =dormArray[1];
		$("dorm_roomtype").innerHTML = dormArray[2];
		 if(dormArray[3]=="0"){
		    $("dorm_sex").innerHTML = "女";
		 }else{
		     $("dorm_sex").innerHTML = "男";
		 }
		$("dorm_availmount").innerHTML = dormArray[4];
		$("chk_dormid").style.display="block";
		}
	
	//获得一个可用学生宿舍列，有分页的返回
	var mygetAllAvailDormback = function setAllDormList(dlist){
		if(dlist==null){
			if($('dataTable').style.visibility == 'visible'){
				$('dataTable').style.visibility = 'hidden';
			}
			if($('pageTable').style.visibility == 'visible'){
				$('pageTable').style.visibility = 'hidden';
			}
			if($('resultTable').style.visibility == 'visible'){
				$('resultTable').style.visibility = 'hidden';
			}
			alert("查询可用宿舍结果为空！");
			return;
		}
		//对按照公寓和宿舍名字查询结果div做处理
		$("selectdormselect").style.display="none"; 
		$("ifDorm").checked==false; //干掉check
		
		if($('dataTable').style.visibility == 'hidden'){
			$('dataTable').style.visibility = 'visible';
		}
		if($('pageTable').style.visibility == 'hidden'){
			$('pageTable').style.visibility = 'visible';
		}
		if($('resultTable').style.visibility == 'hidden'){
			$('resultTable').style.visibility = 'visible';
		}
		
		DWRUtil.removeAllRows('ttb', { filter:function(tr) {
		      return (tr.id != "pattern");
			    }});
		 var dorm, id;
		 for (var i =0; i < dlist.length ; i++) {
		    dorm = dlist[i];
		    id = dorm[0];
		    dwr.util.cloneNode("pattern", { idSuffix:id });
		    dwr.util.setValue("tableid" + id, dorm[0]);
		    dwr.util.setValue("tablename" + id, dorm[1]);
		     if(dorm[2]==0){
		      dwr.util.setValue("tablesex" + id, "女");
		     }else{
		       dwr.util.setValue("tablesex" + id, "男");
		     }
		     dwr.util.setValue("cbx" + id, dorm[0]);
		     dwr.util.setValue("tableavailno" + id, dorm[3]);
		     dwr.util.setValue("tableroomtype" + id, dorm[4]);
		       
		   	 $("pattern" + id).style.display= "";
             dormCache[id] = dlist;
             $("cbx"+id).checked=false; //所有radio不选
		   }
			setPageInfo();
			tbCSS();
		}

	/**
	* 可用宿舍列表的checkox选中是干的事情
	*/
	function test(eleid){
		//得到的eleid为"cbx{id}"
		  gdorm_id =eleid.substring(3);
		  //alert(gdorm_id);
		}
	
	function editClicked(eleid) {
		  //得到的eleid为"edit{id}"，如 "edit42".,经过substring后为42,person的id
		  alert(eleid.substring(4));
		}
	
	function setPageInfo(){
        $('pageTd').innerHTML = '';
        studentLodgeMgrAction.getPage({     
            callback:function(data){
                page = data;
                if(page.totalPage != 0){
                    var pageSpan = document.createElement('span');
                    pageSpan.innerHTML = '共 '+totalcount+' 条 '+'   第 '+page.currentPage+' 页    共'+page.totalPage+' 页 ';
                    $('pageTd').appendChild(pageSpan);
                    var aFirst = document.createElement('a');
                    aFirst.setAttribute('href','javascript:getFirstData()');
                    aFirst.innerHTML = '首页';
                    $('pageTd').appendChild(aFirst);
                    var aPrevious = document.createElement('a');
                    aPrevious.setAttribute('href','javascript:getPreviousData()');
                    aPrevious.innerHTML = '上一页 ';
                    $('pageTd').appendChild(aPrevious);
                    var aNext = document.createElement('a');
                    aNext.setAttribute('href','javascript:getNextData()');
                    aNext.innerHTML = '下一页 ';
                    $('pageTd').appendChild(aNext);
                    var aLast = document.createElement('a');
                    aLast.setAttribute('href','javascript:getLastData()');
                    aLast.innerHTML = '尾页';
                    $('pageTd').appendChild(aLast);    
                }               
            }
        });
        $('loading').style.display = 'none';
    } 
	
	function getFirstData(){
		page.currentPage = 1;
		getDataPerPage();
	}
	
	 function getPreviousData(){
        if(page.currentPage>1){
            page.currentPage--;
        }
        getDataPerPage();
    }
    
    function getNextData(){
        if(page.currentPage<page.totalPage){
            page.currentPage++;
        }
        getDataPerPage();
    }
	
	function getLastData(){
		page.currentPage = page.totalPage;
		getDataPerPage();
	}
	
	
	function getDataPerPage(){
		$('loading').style.display = 'block';
		studentLodgeMgrAction.getAvailDromPerPageByApartmentID(page.currentPage,aprt_id,floorno,dormitoryconcretefuncid,totalcount,mygetAllAvailDormback);
	}	
	
	//点击分配按钮进行分配
	function submitAllocate() {
	var studentidno = $("student_id").innerHTML;
	if(studentidno==""){
		alert('请选择学生');
		return;
		}else if(($("ifDorm").checked==false)&&(gdorm_id=="")){
		alert('请选择公寓');
		return;
		}else{
			//s:datetimepicker的取值方法
			var startindate= dojo.widget.byId("startInDate").getValue();
			var endoutdate= dojo.widget.byId("endOutDate").getValue();
			var dormidno;
			if(gdorm_id==""){
				dormidno = $("dorm_id").innerHTML;
			}else{
				dormidno = gdorm_id;
				//alert("dormidno  is==="+dormidno)
				}
				if(startindate==""){
					alert("请选择开始入住时间");
					}else if(endoutdate==""){
						alert("请选择结束入住时间");
						}else{			
							//给人安排住宿啦！
							dormitoryAllocateAction.allocateTEMP(studentidno,dormidno,startindate,endoutdate,myallocatedback);
						}
		}
	}
	//分配的callback，返回成功与否
	var myallocatedback = function setStatus(status){
			alert(status);
			if(typeof(stu_BasicInfo) != "undefined"){
				stu_BasicInfo();
			}
	}

	
</script>
	</head>
	<body>
		<div id="page_container" style="height:auto;">
			<%@include file="allocatebanner.jsp"%>
			<h2>
				临时人员单人分配
			</h2>
			<!-- 引入学生基本信息页面 -->
			<jsp:include page="basicinfo.jsp"></jsp:include>

			<div id="allocateoneperson_dorm_search">
				<table>
					<tr>
						<td>
							公寓
							<!-- 公寓 -->
							<select class="common_select" id="aptId" name="apartment" style="width: 130px;" onchange="javascript:selectChange();">
								<option value=""></option>
							</select>
						</td>
						<td>
							楼层
							<!-- 楼层 -->
							<select class="common_select" id="floorno" name="floorno" style="width: 120px;">
								<option value=""></option>
							</select>
						</td>
						<td>
							宿舍
							<!-- 宿舍 -->
							<input type="text" id="dormname" size=9; />
						</td>
						<td align="center">
							<div align="left">
								<span><input class="common_button" type="submit" value=查询
										onclick="javascript:getAvailDormbyAptIdDormName()"> </span>
							</div>
						</td>
					</tr>
				</table>

				<!-- 可用公寓查询结果显示 -->
				<div id="selectdormselect">
					<table id="resultTable" style="visibility: hidden;" class="table_common table_list_style" width="600px;">
					<thead>
						<tr>
							<th>
								选择
							</th>
							<th>
								宿舍名
							</th>
							<th>
								住宿性别
							</th>
							<th>
								房型
							</th>
							<th>
								可住人数
							</th>
						</tr>
					</thead>
					<tbody >
						<tr>
							<td>
								<div id="chk_dormid" style="display: none">
									<input type="radio" name="ifDorm" id="ifDorm">
								</div>
								<div id="dorm_id" style="display: none"></div>
							</td>
							<td>
								<div id="dorm_name"></div>
							</td>
							<td>
								<div id="dorm_sex"></div>
							</td>
							<td>
								<div id="dorm_roomtype"></div>
							</td>
							<td>
								<div id="dorm_availmount"></div>
							</td>
						</tr>
						</tbody>
					</table>
				</div>
				<table id="dataTable" style="visibility: hidden;" class="table_common table_list_style">
					<thead>
						<tr>
							<th>
								选择
							</th>
							<th >
								宿舍名
							</th>
							<th >
								住宿性别
							</th>
							<th >
								房型
							</th>
							<th >
								可住人数
							</th>
						</tr>
					</thead>
					<tbody id="ttb" >
						<tr id="pattern" style="display: none;">
							<td>
								<input type="radio" id="cbx" name="cbx" onclick="test(this.id)" />
								<br />
							</td>
							<span id="tableid" style="display: none">id</span>
							<td>
								<span id="tablename">name</span>
							</td>
							<td>
								<span id="tablesex">sex</span>
							</td>
							<td>
								<span id="tableroomtype"
									>roomtype</span>
							</td>
							<td>
								<span id="tableavailno"
									>availno</span>
							</td>
						</tr>
					</tbody>
				</table>
				<table id="pageTable" 
					style="visibility: hidden; margin-top: 30px; font: 12px;"
					class="navigator">
					<tr>
						<td id="pageTd">
						</td>
					</tr>
				</table>
				<div id="loading"
					style="display: none; margin-top: 20px; text-align: center; color: red; font: 13px;">
					Loading...
				</div>
				<!-- 可用公寓查询结果显示结束 -->
			</div>
			<!-- allocateoneperson_dorm_search -->

			<!-- 入住时间table开始 -->
			<div id="startt">
				开始入住时间：
				<s:datetimepicker name="startInDate" id="startInDate"
					value="today" displayFormat="yyyy-MM-dd" />
			</div>

			<div id="endt">
				结束入住时间：
				<s:datetimepicker name="endOutDate" id="endOutDate"
					displayFormat="yyyy-MM-dd" />
			</div>
			<!-- 入住时间table结束 -->

			<div id="outlier"></div>
			<div >
				<input class="common_button" type="submit" value=分配
					onclick="javascript:submitAllocate()" />
			</div>
		</div>
		<script type="text/javascript">	
			window.onload = function(){
				var nav_a=$j('#page_nav a');
				var nav_span=$j("#page_nav a span");
				for(var u = 0 ;u<nav_a.length;u++){
						nav_a.get(u).style.backgroundPosition="left top";
						nav_span.get(u).style.backgroundPosition="right top";							
				}
				$j('#page_nav a').get(0).style.backgroundPosition="0% -42px";
				$j("#page_nav a span").get(0).style.backgroundPosition="100% -42px";
				setApartList();
			}	
		</script>
	</body>
</html>
